package cn.ygh.bbo.system.web.aspect;


import cn.ygh.bbo.common.annotation.Log;
import cn.ygh.bbo.common.utils.JsonUtils;
import cn.ygh.bbo.framerwork.web.WebUtils;
import cn.ygh.bbo.system.bean.Admin;
import cn.ygh.bbo.system.bean.OpretationLog;
import cn.ygh.bbo.system.sdk.shiro.ShiroUtils;
import cn.ygh.bbo.system.service.OpretationLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author ygh
 * @Desc  后台通用日志切面
 * @date 2018/7/1 22:28
 */
@Aspect
@Component
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Autowired
    OpretationLogService opretationLogService;

    @Pointcut(value = "@annotation(cn.ygh.bbo.common.annotation.Log)")
    private void pointCut() {}

    /**
     * 调用成功记录一下日志
     *
     * @throws IOException
     */
    @AfterReturning(value = "pointCut()", returning = "object")
    public void requestLimitReturning(JoinPoint joinPoint, Object object) throws Exception {
        //权限注解获取
        Log log = getAnnotationLog(joinPoint);
        //请求响应
        HttpServletRequest request = WebUtils.getRequest();
        Admin admin = ShiroUtils.getPrincipal();
        //备注 记录请求和响应  以 | 分割
        String requestStr="";
        if (log.request()){
            if (request.getParameterMap()!=null){
                requestStr=JsonUtils.toJSONString(request.getParameterMap());
            }
            if (requestStr.length() > 1000) {
                requestStr = requestStr.substring(0, 999);
            }
        }
        String responseStr="";
        if (log.response()){
            responseStr=JsonUtils.toJSONString(object);
            if (responseStr.length() > 1000) {
                responseStr = responseStr.substring(0, 999);
            }
        }
        //id
        String tbid=request.getParameter("id");
        //管理员操作日志记录
        OpretationLog opretationLog = new OpretationLog(
                log.value() //表名
                ,tbid //表序号
                ,admin.getId() //管理员序号
                ,log.operationId() //类型
                ,WebUtils.getUri() //操作地址
                ,WebUtils.getIp() //管理员ip
                ,requestStr //请求数据
                ,responseStr //响应数据
                ,new Date() //创建时间
        );
        opretationLogService.save(opretationLog);
    }

    /**
     * 是否存在注解，如果存在就获取
     */
    private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method.getAnnotation(Log.class);
        }
        return null;
    }


}